package com.keepitsimple.core;

import java.io.File;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public final class DispatcherServletHelper {

	public static Map<String, ActionContext> urls = null;

	public static void init(String scan_package)
	{
		if (null == scan_package || scan_package.length() <= 0)
			System.out.println("正在扫描classpath....");
		urls = new HashMap<String, ActionContext>();
		String result = DispatcherServletHelper.class.getResource("/").toString().replace("file:/", "").replace("%20", "");
		try
		{
			assembleBean(result, result.replace("/", "\\"), scan_package);
		} catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	 * 扫描该路径下 所有的class文件 then 装配bean
	 * 
	 * @param realPath
	 * @param root
	 *            真实路径 E:\\GitHome\\kis\\build\\classes\\
	 * @param scan_package
	 *            扫描的包
	 * @return
	 * @throws Exception
	 * @throws
	 */
	public static String assembleBean(String realPath, String root, String scan_package) throws Exception
	{
		File file = new File(realPath);
		if (file.isDirectory())
		{
			for (File f : file.listFiles())
			{
				if (f.isDirectory())
				{
					assembleBean(f.getAbsolutePath(), root, scan_package);
				} else
				{
					String classPath = f.getAbsolutePath().replace("\\", ".");
					int len = classPath.length();
					if (len > 6 && classPath.endsWith(".class"))
					{
						if(null!=scan_package&&scan_package.length()>0&&classPath.indexOf("classes."+scan_package)>=0){
							classPath = classPath.substring(8+classPath.indexOf("classes."+scan_package));
						}else{
							continue;
						}
						String className = classPath.substring(0, classPath.length() - 6);
						Class<?> clazz = Class.forName(className);
						Method[] methods = clazz.getDeclaredMethods();
						for (Method method : methods)
						{
							if (method.isAnnotationPresent(com.keepitsimple.core.ActionAnnotate.class))
							{
								com.keepitsimple.core.ActionAnnotate actionAnno = method.getAnnotation(com.keepitsimple.core.ActionAnnotate.class);
								if (urls.containsKey(actionAnno.ActionName()))
								{
									throw new RuntimeException("重复路径" + clazz + ":" + actionAnno.ActionName());
								} else
								{
									ActionContext bean = new ActionContext();
									bean.setCla(clazz);
									bean.setMethodName(method.getName());
									bean.setURL(actionAnno.ActionName());
									Result[] results = actionAnno.Results();
									Map<String,String> map = new HashMap<String, String>();
									for (Result result : results)
									{
										map.put(result.name(), result.value());
									}
									bean.setResults(map);
									bean.setAction(clazz.newInstance());
									bean.setParamsType(method.getParameterTypes());
									bean.setActionParamsName(Utils.getParamNames(clazz, method.getName()));
									urls.put(actionAnno.ActionName(), bean);
								}
							}
						}
					}
				}
			}
		}
		return "";
	}

}
